package com.fancy.learncenter.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.fancy.learncenter.adapter.ToothFJAdapter;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.NearbyDataBean;
import com.fancy.learncenter.bean.TzDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.view.CustomFootView;
import com.fancy.learncenter.view.CustomHeaderView;
import com.fancy.learncenter.view.PopupWindowEducationType;
import com.fancy.learncenter.view.PopupWindowWorkType;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ToothFJFragment extends Fragment {


    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;
    @Bind(R.id.recycleView)
    RecyclerView recycleView;

    ArrayList<TzDataBean> mData;
    @Bind(R.id.prompt_img)
    ImageView promptImg;
    @Bind(R.id.shaixuan)
    LinearLayout shaixuan;
    @Bind(R.id.sex_tv)
    TextView sexTv;
    @Bind(R.id.work_tv)
    TextView workTv;

    public ToothFJFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_tooth_nearby, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    PopupWindowWorkType windowWorkType;
    ToothFJAdapter adapter;

    private void initView() {
        mData = new ArrayList<>();
        xRefreshView.setPullRefreshEnable(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);
        adapter = new ToothFJAdapter(getActivity(), new ArrayList<NearbyDataBean>());
        recycleView.setAdapter(adapter);

        adapter.setCustomLoadMoreView(new CustomFootView(getActivity()));
        xRefreshView.setPullLoadEnable(true);
        xRefreshView.setCustomHeaderView(new CustomHeaderView(getActivity()));
        xRefreshView.setPinnedTime(1000);
        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
            @Override
            public void onRefresh() {
                getDatas(true);
            }

            @Override
            public void onRefresh(boolean isPullDown) {

            }

            @Override
            public void onLoadMore(boolean isSilence) {
                getDatas(false);
                LogUtil.MyLog("onLoadMore", "====onLoadMore==");
            }

            @Override
            public void onRelease(float direction) {

            }

            @Override
            public void onHeaderMove(double headerMovePercent, int offsetY) {

            }
        });

        getDatas(true);
    }

    int page = 0;

    private void getDatas(final boolean isRefresh) {
        Map map = new HashMap();


        if (isRefresh) {
            page = 0;
        } else {
            page++;
        }
        map.put("page", page + "");

        map.put("lng", MyApplication.longitude);

        map.put("lat", MyApplication.latitude);


        HttpMehtod.getInstance().nearby(map, new IdeaObserver<BaseDataBean<List<NearbyDataBean>>>(getActivity(), true) {

            @Override
            public void onSuccess(BaseDataBean<List<NearbyDataBean>> listBaseDataBean) {


                if (isRefresh) {
                    if (xRefreshView == null) {
                        return;
                    }
                    xRefreshView.stopRefresh();
                    xRefreshView.setLoadComplete(false);
                    if (listBaseDataBean.getData() == null) {
                        promptImg.setVisibility(View.VISIBLE);
                        adapter.notifyDataSetChanged(new ArrayList<NearbyDataBean>());
                        return;
                    } else {
                        promptImg.setVisibility(View.GONE);
                        adapter.notifyDataSetChanged((ArrayList<NearbyDataBean>) listBaseDataBean.getData());
                    }

                } else {
                    if (listBaseDataBean.getData() == null) {
                        xRefreshView.setLoadComplete(true);
                    } else if (listBaseDataBean.getData() != null && listBaseDataBean.getData().size() < Constant.PAGE_NUM) {
                        xRefreshView.setLoadComplete(true);
                    } else {
                        xRefreshView.stopLoadMore();
                    }


                    if (listBaseDataBean.getData() == null) {
                        return;
                    }
                    adapter.addAll(listBaseDataBean.getData());
                }


            }

            @Override
            public void onFail(int errorCode) {
                if (xRefreshView != null) {
                    xRefreshView.stopRefresh();
                    xRefreshView.setLoadComplete(false);
                }
            }
        });
    }

    @OnClick({R.id.sex_type, R.id.work_type})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sex_type:
                break;
            case R.id.work_type:

                if (windowWorkType == null) {
                    windowWorkType = new PopupWindowWorkType(getActivity(), shaixuan);
                    windowWorkType.setClickCallBack(new PopupWindowEducationType.ClickCallBack() {
                        @Override
                        public void cancleCallBack() {

                        }

                        @Override
                        public void selecCallBack(String content, String id) {
                            workTv.setText(content);
                        }


                    });
                }
                windowWorkType.showPopupWindow();
                break;
        }
    }
}
